package com.springboot.shanhexin.controller;

import com.springboot.shanhexin.entity.*;
import com.springboot.shanhexin.service.qiantaiService;
import com.springboot.shanhexin.unit.GreedAlo;
import com.springboot.shanhexin.unit.TencentMapAPIUtils;
import com.springboot.shanhexin.unit.getOpenIdutil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class qiantaiCtrl {

    @Autowired
    private qiantaiService qiantaiService;

    //获取景区内所有景点经纬度及id
    @RequestMapping("/qiantai/all_place")
    @ResponseBody
    public String all_place(String name) {
        List<place> placeList=new ArrayList<place>();
        JSONObject jsonObject = new JSONObject();
        placeList=qiantaiService.getallplace(name);
        jsonObject.put("code", "1");
        jsonObject.put("message", placeList);
        return jsonObject.toString();
    }

    //获取某个景点的所有信息
    @RequestMapping("/qiantai/one_place")
    @ResponseBody
    public String one_place(int id) {
        List<place> placeList=new ArrayList<place>();
        JSONObject jsonObject = new JSONObject();
        placeList=qiantaiService.getplaceone(id);
        jsonObject.put("code", "1");
        jsonObject.put("message", placeList);
        return jsonObject.toString();
    }

    //添加用户
    @RequestMapping("/qiantai/add_user")
    @ResponseBody
    public String add_user(String name,String openid) {
        int user=0;
        JSONObject jsonObject = new JSONObject();
        user=qiantaiService.getuserCount(openid);
        if (user == 0) {
            qiantaiService.adduseropenid(openid);
        }
        qiantaiService.addusername(name, openid);
        jsonObject.put("code", "1");
        return jsonObject.toString();
    }

    //添加景点
    @RequestMapping("/qiantai/add_place")
    @ResponseBody
    public String add_place(String title,String openid,String place_name) {
        int place=0;
        JSONObject jsonObject = new JSONObject();
        place=qiantaiService.gettouristCount(openid, title, place_name);
        if (place == 0) {
            qiantaiService.addtourist(openid,title,place_name);
        }
        jsonObject.put("code", "1");
        return jsonObject.toString();
    }

    //显示景点附近的餐厅
    @RequestMapping("/qiantai/one_res")
    @ResponseBody
    public String one_res(int id) {
        List<restaurant> restaurantList=new ArrayList<restaurant>();
        JSONObject jsonObject = new JSONObject();
        restaurantList=qiantaiService.getresone(id);
        jsonObject.put("code", "1");
        jsonObject.put("message", restaurantList);
        return jsonObject.toString();
    }

    //添加餐厅
    @RequestMapping("/qiantai/add_res")
    @ResponseBody
    public String add_res(String title,String openid,String place_name,String name,int rid) {
        int res=0;
        JSONObject jsonObject = new JSONObject();
        int tid=0;
        tid=qiantaiService.searchtid(openid,title,place_name);
        if (tid > 0) {
            res=qiantaiService.getmy_resCount(tid, name,rid);
            if (res == 0) {
                qiantaiService.addmy_res(tid,name,rid);
            }
            jsonObject.put("code", "1");
            return jsonObject.toString();
        }else {
            jsonObject.put("code", "3");
            return jsonObject.toString();
        }
    }

    //显示景点附近的酒店
    @RequestMapping("/qiantai/one_hot")
    @ResponseBody
    public String one_hot(int id) {
        List<hotel> hotelList=new ArrayList<hotel>();
        JSONObject jsonObject = new JSONObject();
        hotelList=qiantaiService.getholone(id);
        jsonObject.put("code", "1");
        jsonObject.put("message", hotelList);
        return jsonObject.toString();
    }

    //添加酒店
    @RequestMapping("/qiantai/add_hot")
    @ResponseBody
    public String add_hot(String title,String openid,String place_name,String name,int hid) {
        int hol=0;
        JSONObject jsonObject = new JSONObject();
        int tid=0;
        tid=qiantaiService.searchtid(openid,title,place_name);
        if (tid > 0) {
            hol=qiantaiService.getmy_holCount(tid, name,hid);
            if (hol == 0) {
                qiantaiService.addmy_hol(tid,name,hid);
            }
            jsonObject.put("code", "1");
            return jsonObject.toString();
        }else {
            jsonObject.put("code", "3");
            return jsonObject.toString();
        }
    }

    //拿到所有定制路线
    @RequestMapping("/qiantai/all_tourist")
    @ResponseBody
    public String all_tourist(String openid) {
        JSONObject jsonObject = new JSONObject();
        List<tourist> touristList=new ArrayList<tourist>();
        touristList=qiantaiService.getalltourist(openid);
        jsonObject.put("code", "1");
        jsonObject.put("message", touristList);
        return jsonObject.toString();
    }

    //获取指定路线
    @RequestMapping("/qiantai/one_tourist")
    @ResponseBody
    public String one_tourist(String openid,String title) {
        JSONObject jsonObject = new JSONObject();
        List<tourist> touristList=new ArrayList<tourist>();
        touristList=qiantaiService.getonetourist(openid,title);
        jsonObject.put("code", "1");
        jsonObject.put("message", touristList);
        return jsonObject.toString();
    }

    //拿到所选路线上的餐厅和酒店
    @RequestMapping("/qiantai/all_res_hol")
    @ResponseBody
    public String all_res_hol(String uid,String my_rode) {
        JSONObject jsonObject = new JSONObject();
        List<hotel> hotel=new ArrayList<hotel>();
        List<restaurant> restaurant=new ArrayList<restaurant>();
        hotel=qiantaiService.getmy_hol_wx(uid,my_rode);
        restaurant=qiantaiService.getmy_res_wx(uid,my_rode);
        jsonObject.put("code", "1");
        jsonObject.put("hotel", hotel);
        jsonObject.put("restaurant", restaurant);
        return jsonObject.toString();
    }

    //拿到用户的openid
    @RequestMapping("/qiantai/get_openid")
    @ResponseBody
    public String get_openid(String code) {
        getOpenIdutil getopenid = new getOpenIdutil();
        String jsonId = getopenid.getopenid("wxbcf47320719a3126", code, "2e1efe4e52b4d2eb214b3072e4d69059");
        JSONObject jsonObject1 = JSONObject.fromObject(jsonId);
        //从json字符串获取openid和session_key
        String openid=jsonObject1.getString("openid");
        String session_key=jsonObject1.getString("session_key");
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("openid", openid);
        return jsonObject2.toString();
    }

    //获取贪心路径
    @RequestMapping("/qiantai/getdistance")
    @ResponseBody
    public String getGreedtourist(String openid,String title) throws IOException {
        //拿到景点个数
        int num=qiantaiService.gettouristSpotCount(openid,title);
        //拿到景点名称数组
        String[] spot=qiantaiService.gettouristSpotName(openid,title);
        //拿到景点经纬度
        String from=qiantaiService.gettouristSpotPosition(spot);
        //计算距离矩阵
        String to=from;
        TencentMapAPIUtils tencentMapAPIUtils = new TencentMapAPIUtils();
        int[][] distances = tencentMapAPIUtils.getDistances(num,from,to);
        //调用算法得出最优路线
        GreedAlo ts = new GreedAlo(num);//景点数目
        ts.init(distances);
        spot=ts.solve(spot);
        //输出的景点数组
        for (int i = 0; i <spot.length ; i++) {
            System.out.println(spot[i]);
        }
        //根据景点数组得到景点id
        String spot_id[]=new String[num];
        for (int i = 0; i <spot.length; i++) {
            spot_id[i]=qiantaiService.get_spotid(spot[i]);
        }
        for (int i = 0; i <spot_id.length; i++) {
            System.out.println("id:"+spot_id[i]);
        }
        //计算最优路线相邻景点的步行时间
        int minute[]=new int[num];
        int minute_temp=0;
        for (int i = 0; i <spot.length-1 ; i++) {
            String onefrom=qiantaiService.getoneSpotPosition(spot[i]);
            String oneto=qiantaiService.getoneSpotPosition(spot[i+1]);
            int[][] onedistances = tencentMapAPIUtils.getDistances(1,onefrom,oneto);
            minute[i]=onedistances[0][0]/70;
            minute_temp=minute_temp+minute[i];
        }
        minute[num-1]=minute_temp;

        //将得到的最优路线传入数据库
        String minuteString="";
        String spotString="";
        for (int i = 0; i <minute.length ; i++) {
            minuteString=minuteString+String.valueOf(minute[i]);
            minuteString=minuteString+";";
        }
        for (int i = 0; i <spot.length ; i++) {
            spotString=spotString+spot[i];
            spotString=spotString+";";
        }
        qiantaiService.addnewtourist(openid,title,spotString,minuteString);
        //输出到前台最优路径
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "1");
        jsonObject.put("spot", spot);
        jsonObject.put("spot_id", spot_id);
        jsonObject.put("minute", minute);
        return jsonObject.toString();
    }

    //拿贪心路径经纬度(latitude)
    @RequestMapping("/qiantai/getLatitude")
    @ResponseBody
    public String[] getGreedtouristLat(String[] spot){
        String[] latitude = qiantaiService.getGreedtouristLat(spot);
        return latitude;
    }

    //拿贪心路径经纬度（longitude）
    @RequestMapping("/qiantai/getLongitude")
    @ResponseBody
    public String[] getGreedtouristLong(String[] spot){
        String[]  longitude= qiantaiService.getGreedtouristLong(spot);
        return longitude;
    }
    //Delete接口
    @RequestMapping("/qiantai/delete")
    @ResponseBody
  public String getDelete_tourist(String uid,String title){
        JSONObject jsonObject=new JSONObject();
        qiantaiService.getDelete_tourist(uid,title);
        qiantaiService.getDelete_tourist_route(uid, title);
        String [] tid=qiantaiService.get_tid(uid,title);
        for (int i = 0; i <tid.length ; i++) {
            qiantaiService.getDelete_my_res(tid[i]);
            qiantaiService.getDelete_my_hol(tid[i]);
        }
        jsonObject.put("delete", "success");
        return jsonObject.toString();
        }
    }
